package com.icss.web.back.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.icss.common.controller.BaseController;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.users.service.IUsersDbService;
import com.icss.users.vo.ReAdmins;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 登陆验证的拦截器
 * 
 * @author 刘鑫
 */
@Component("authBackInterceptor")
public class AuthBackInterceptor<T> extends BaseController implements HandlerInterceptor
{
	@Resource
	private IUsersDbService usersDbService;

	/**
	 * 先执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		ConstatFinalUtil.SYS_LOGGER.info("--授权拦截器执行--");
		/* 加载一些公共的变量 */
		this.commonOper(request);
		String currentUrl = request.getRequestURL() + "";
		String queryStr = request.getQueryString();
		HttpSession session = request.getSession();
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		/* 获取用户对象 */
		ReAdmins admins = (ReAdmins) this.findObjfromSession(request, "admins");
		if (admins == null)
		{
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			/* 提示信息 */
			session.setAttribute("response", apiResponse.toJSON());
			/* 提示信息 */
			response.sendRedirect(request.getContextPath() + "/login.htm");
			return false;
		}
		
		if (admins.getId() == 1)
		{
			/* 如果管理员的id为1,默认是超级管理员,直接放行 */
			return true;
		}
		return true;
	}
}
